﻿using LD.Host;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class TaskToolFrm : FrmToolBase
    {
        public TaskToolFrm()
        {
            InitializeComponent();
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += FrmTaskTool_Shown;
        }

        private void FrmTaskTool_Shown(object sender, EventArgs e)
        {
            Init();

            if (this.TB_Tool.Param != null)
            {
                List<TaskToolModel> taskModels = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TaskToolModel>>(this.TB_Tool.Param);

                foreach (var item in taskModels)
                {
                    int index = this.dgv_data.Rows.Add();
                    this.dgv_data["name", index].Value = item.Name;
                    this.dgv_data["wait", index].Value = item.Wait;

                    var isExist = Appbus.PartNo.Config.Flow.Where(p => p.Name == item.Flow).FirstOrDefault();
                    if (isExist != null)
                    {
                        DataGridViewComboBoxCell cobCellName = this.dgv_data["Flow", index] as DataGridViewComboBoxCell;
                        cobCellName.Value = item.Flow;
                    }
                }
            }
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            List<TaskToolModel> taskModels = new List<TaskToolModel>();

            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                string name = item.Cells["name"].Value.ToString();
                string flow = item.Cells["Flow"].Value.ToString();
                bool wait = Convert.ToBoolean(item.Cells["wait"].Value);
                taskModels.Add(new TaskToolModel()
                {
                    Name = name,
                    Flow = flow,
                    Wait = wait
                });
            }
            this.TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(taskModels);
            this.Close();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.dgv_data.Rows.Add();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void dgv_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.删除ToolStripMenuItem.Visible = false;
        }

        private void dgv_data_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.删除ToolStripMenuItem.Visible = true;
        }

        private void Init()
        {
            this.Flow.DataSource = Appbus.PartNo.Config.Flow.Where(p => p.FlowType == 1).ToList();
            this.Flow.DisplayMember = "name";
            this.Flow.ValueMember = "name";
        }
        public void Delete()
        {
            if (DialogResult.Yes == MessageBox.Show("你确定要删除所选内容", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                DataGridViewSelectedRowCollection rows = this.dgv_data.SelectedRows;
                foreach (DataGridViewRow item in rows)
                {
                    this.dgv_data.Rows.Remove(item);

                }
            }
        }
    }
}
